#include"Addr.hpp"
#include"common.hpp"
//调用方式:./tcpclient ip port
int main(int argc,char* argv[])
{
    if(argc!=3)
    {
        std::cout<<"调用方式错误,正确方式为:./tcplient ip port"<<std::endl;
        exit(Sock::usageerr);
    }
    int sockfd=socket(AF_INET,SOCK_STREAM,0);//创建异端
    if(sockfd<0)
    {
        std::cout<<"客户端创建异端失败"<<std::endl;
        exit(Sock::sockerr);
    }
    uint16_t port=std::stoi(argv[2]);
    std::string ip=argv[1];
    Addr client(ip,port);
    int n=connect(sockfd,(struct sockaddr*)client.pSockaddr(),client.Socklen());
    if(n<0)
    {
        std::cout<<"客户端连接失败"<<std::endl;
        exit(Sock::connecterr);
    }
    while(true)
    {
        std::cout<<"please enter:";
        std::string con;
        getline(std::cin,con);
        int w=write(sockfd,con.c_str(),con.size());
        char buffer[1024];
        ssize_t r=read(sockfd,buffer,sizeof(buffer)-1);
        if(r>0)
        {
            buffer[r]=0;
            std::cout<<buffer;
        }
    }
    close(sockfd);
    return 0;
}